package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.common.job.CupBfJob;
import org.footballmanager.pandafootball.common.job.QuartzManager;
import org.footballmanager.pandafootball.common.util.DateUtils;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootCupbf;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.enums.BFLX;
import org.footballmanager.pandafootball.system.domain.enums.BFZT;
import org.footballmanager.pandafootball.system.form.impl.FootCupbfSearchForm;
import org.footballmanager.pandafootball.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SessionAttributes("footCupbfSearchForm")
@RequestMapping("/hy/footCupbf")
public class HyCupbfController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footCupbf";
    private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.HY, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);

    @Autowired
    private FootCupbfService footCupbfService;
    @Autowired
    private FootCupbmService footCupbmService;
    @Autowired
    private FootSjService footSjService;
    @Autowired
    private FootZnxService footZnxService;
    @Autowired
    private FootTeamService footTeamService;
    @Autowired
    private FootClubService footClubService;
    @Autowired
    private FootUserPersonService footUserPersonService;

    /**
     * 初始化
     *
     * @param map                 参数
     * @param footCupbfSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootCupbfSearchForm footCupbfSearchForm) {
        map.addAttribute("sjList", footSjService.getall());
        map.addAttribute("teamList", footTeamService.getall());
        FootCupbfSearchForm newFootCupbfSearchForm = new FootCupbfSearchForm();
        if (footCupbfSearchForm != null) {
            newFootCupbfSearchForm = footCupbfSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootCupbfSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, HttpServletRequest request,
                         @ModelAttribute FootCupbfSearchForm footCupbfSearchForm) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        boolean type = footClubService.findNumByUserId(user.getId());
        footCupbfSearchForm.setBflx(type ? BFLX.WINNER : BFLX.ALLIANCE);
        footCupbfSearchForm.setSj(footSjService.getSj());
        footCupbfService.search(ENTITYNAME, map, footCupbfSearchForm);
        map.addAttribute("title", footCupbfSearchForm.getBflx().name());
        return LISTPAGE;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String openAddPage(Model map, HttpServletRequest request) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        //查找可以报分的玩家
        FootCupbf fc = footCupbfService.findCupbfByUserId(user.getId());
        if (null != fc) {
            map.addAttribute(ENTITYNAME, fc);
            map.addAttribute("zhuTeam", footUserPersonService.findFootPersonByUserId(fc.getQd1(), request));
            map.addAttribute("keTeam", footUserPersonService.findFootPersonByUserId(fc.getQd2(), request));
        }
        return ADDPAGE;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String save(@ModelAttribute FootCupbf footCupbf,
                       BindingResult bindingResult, HttpServletRequest request, Model map, SessionStatus s) {
        if (footCupbf.getJq1().equals(footCupbf.getJq2())) {
            bindingResult.rejectValue("qd1", Constants.MISFORMAT, "胜负未分啊");
        }
        List<FootPerson> zhulist = footUserPersonService.findFootPersonByUserId(footCupbf.getQd1(), request);
        List<FootPerson> kelist = footUserPersonService.findFootPersonByUserId(footCupbf.getQd2(), request);
        if (zhulist.size() < 18 && kelist.size() < 18) {
            bindingResult.rejectValue("qd1", Constants.MISFORMAT, "对阵双方人数不足，无法进行报分操作");
        }

        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        if (!bindingResult.hasErrors()) {
            if (zhulist.size() >= 18 && kelist.size() >= 18) {
                footCupbf.setBfzt(BFZT.NOT);
                footCupbf.setBfUserId(user.getId());
                footCupbfService.updateNotNull(footCupbf, request);
                if (user.getId().equals(footCupbf.getQd1())) {
                    footZnxService.sendZnx(footCupbf.getQd1(), footCupbf.getQd2(), "请确认" + footCupbf.getBflx().name() + "报分结果",
                            footTeamService.getLogoByUserid(user.getId()), "/hy/footCupbf/mysearch", request);
                } else {
                    footZnxService.sendZnx(footCupbf.getQd2(), footCupbf.getQd1(), "请确认" + footCupbf.getBflx().name() + "报分结果",
                            footTeamService.getLogoByUserid(user.getId()), "/hy/footCupbf/mysearch", request);
                }
            } else {
                footCupbf.setBfzt(BFZT.HAVE);
                footCupbf.setBfUserId(user.getId());
                footCupbfService.updateNotNull(footCupbf, request);
                footCupbfService.atOnceIn(footCupbf, request);
            }
            s.setComplete();
            return "redirect:/hy/footCupbf/bettle";
        } else {
            //查找可以报分的玩家
            map.addAttribute(ENTITYNAME, footCupbf);
            map.addAttribute("zhuTeam", zhulist);
            map.addAttribute("keTeam", kelist);
            return ADDPAGE;
        }
    }

    @RequestMapping(value = "/mySearch", method = RequestMethod.GET)
    public String mySearch(Model map, HttpServletRequest request) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        map.addAttribute(ENTITYNAME, footCupbfService.findMakesureBfByUserid(user.getId()));
        return "hy/footCupbf/MyList";
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footCupbfService.get(id));
        return VIEWPAGE;
    }

    @RequestMapping(value = "/makesure", method = RequestMethod.GET)
    public String makesure(@RequestParam Long id, HttpServletRequest request, SessionStatus s) {
        FootCupbf bf = footCupbfService.get(id);
        footCupbfService.makesure(bf, request);
        s.setComplete();
        return "redirect:/hy/footCupbf/mysearch";
    }

    @RequestMapping(value = "/bettle", method = RequestMethod.GET)
    public String bettle(Model map, HttpServletRequest request) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        boolean type = footClubService.findNumByUserId(user.getId());
        footCupbmService.bettle(type, map);
        return "hy/footCupbm/List";
    }

}
